﻿/*
=======================================================
    Bill【单据】
=======================================================
*/

using Entities;
using System.Collections.Generic;

namespace Services
{
    public interface IBillRepository : IRepository<Bill>
    {
        void ConfirmForIn(Bill info, List<BillDetail> billDetails, List<Stock> list);
        void ConfirmForOut(Bill bill, List<BillDetail> billDetails, List<Stock> stockList);
        void DeleteForAdmin(int id);
        int InsertForAdmin(Bill bill, List<BillDetail> billDetails);
        void UpdateForAdmin(Bill bill, List<BillDetail> billDetails);
    }


    public class BillRepository : BaseRepository<Bill>, IBillRepository
    {
        public BillRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<Bill> query)
            : base(dB, currentUser, query)
        {
        }

        #region GetPage
        public override (List<Bill> items, int totalCount) GetPage(Expressionable<Bill> where, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            var fs = _DB.Queryable<Bill, OperType, Warehouse>
                 ((x, ot, wh) => new JoinQueryInfos(
                    JoinType.Inner, x.OperTypeID == ot.OperTypeID,
                    JoinType.Inner, x.WarehouseID == wh.WarehouseID
                 ))
                 .Select((x, ot, wh) => new Bill
                 {
                     BillID = x.BillID.SelectAll(),
                     OperType_Name = ot.Name,
                     Warehouse_Name = wh.Name
                 });

            return _query.GetPage(where, pageIndex, pageSize, orderBy, fs);
        }
        #endregion

        #region GetModel
        public override Bill GetModel(Expressionable<Bill> where, string orderBy = null)
        {
            var fs = _DB.Queryable<Bill, OperType, Warehouse>
                ((x, ot, wh) => new JoinQueryInfos(
                   JoinType.Inner, x.OperTypeID == ot.OperTypeID,
                   JoinType.Inner, x.WarehouseID == wh.WarehouseID
                ))
                .Select((x, ot, wh) => new Bill
                {
                    BillID = x.BillID.SelectAll(),
                    OperType_Name = ot.Name,
                    Warehouse_Name = wh.Name
                });

            return _query.GetModel(where, orderBy, fs);
        }
        #endregion

        #region GetSearch
        public override List<Bill> GetSearch(Expressionable<Bill> where, string orderBy = null, int? top = null)
        {
            var fs = _DB.Queryable<Bill, OperType, Warehouse>
               ((x, ot, wh) => new JoinQueryInfos(
                  JoinType.Inner, x.OperTypeID == ot.OperTypeID,
                  JoinType.Inner, x.WarehouseID == wh.WarehouseID
               ))
               .Select((x, ot, wh) => new Bill
               {
                   BillID = x.BillID.SelectAll(),
                   OperType_Name = ot.Name,
                   Warehouse_Name = wh.Name
               });

            return _query.GetSearch(where, orderBy, top, fs);
        }
        #endregion

        #region DeleteForAdmin
        public void DeleteForAdmin(int id)
        {
            _DB.Deleteable<BillDetail>().Where(x => x.BillID == id).ExecuteCommand();

            Delete(id);
        }
        #endregion

        #region InsertForAdmin
        public int InsertForAdmin(Bill bill, List<BillDetail> billDetails)
        {
            var billID = Insert(bill);

            foreach (var item in billDetails)
            {
                item.BillID = billID;
                item.GroupInfoID = bill.GroupInfoID;
            }

            _DB.Insertable(billDetails).ExecuteCommand();

            return billID;
        }
        #endregion

        #region UpdateForAdmin
        public void UpdateForAdmin(Bill bill, List<BillDetail> billDetails)
        {
            Update(bill);

            _DB.Deleteable<BillDetail>().Where(x => x.BillID == bill.BillID).ExecuteCommand();

            foreach (var item in billDetails)
            {
                item.BillID = bill.BillID;
                item.GroupInfoID = bill.GroupInfoID;
            }

            _DB.Insertable(billDetails).ExecuteCommand();
        }
        #endregion

        #region 入库确认  ConfirmForIn
        public void ConfirmForIn(Bill info, List<BillDetail> billDetails, List<Stock> list)
        {
            Update(info);

            foreach (var item in billDetails)
            {
                _DB.Updateable(item).ExecuteCommand();
            }

            _DB.Insertable(list).ExecuteCommand();
        }
        #endregion

        #region 出库确认  ConfirmForOut
        public void ConfirmForOut(Bill bill, List<BillDetail> billDetails, List<Stock> stockList)
        {
            Update(bill);

            foreach (var item in billDetails)
            {
                _DB.Updateable(item).ExecuteCommand();
            }

            _DB.Updateable(stockList).ExecuteCommand();
        }
        #endregion
    }
}
